package com.mhs.dataclassification.processK;

import com.mhs.DTO.MySQLiteHelper;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ProcessK_ThuocDTO {
	public static void updateK_ThuocDTO(int id_MaThuoc, SQLiteDatabase database, MySQLiteHelper dbHelper){
		int K_Old = 0;
		database = dbHelper.getWritableDatabase();
		String sQueryFirst = "select K from thuoc where mathuocclient = " + id_MaThuoc + ";";
		Cursor cursor = database.rawQuery(sQueryFirst, new String [] {});
		if(cursor.getCount() == 0){
			K_Old = 0;
		}else if(cursor.getCount() == 1){
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				K_Old = cursor.getInt(0);
				break;
			}
			// Make sure to close the cursor
			cursor.close();
		}
		int K_New = K_Old + 1;
		String sQuery = "update thuoc set K = " + K_New + " where mathuocclient = " + id_MaThuoc + ";";		
		database.execSQL(sQuery);
	}
	
	public static int getK_ThuocDTO(int id_MaThuoc, SQLiteDatabase database, MySQLiteHelper dbHelper){
		database = dbHelper.getWritableDatabase();
		int K = 0;
		String sQuery = "select K from thuoc where mathuoc = " + id_MaThuoc + ";";
		Cursor cursor = database.rawQuery(sQuery, new String [] {});
		if(cursor.getCount() == 0){
			K = 0;
		}else if(cursor.getCount() == 1){
			cursor.moveToFirst();
			while (!cursor.isAfterLast()) {
				K = cursor.getInt(0);
				break;
			}
		}
		return K;
	}
}
